package com.group7.mall.controller;

import com.group7.mall.bean.JsonResult;
import com.group7.mall.bean.Result;
import com.group7.mall.dto.*;
import com.group7.mall.entity.TbCommentDiscussion;
import com.group7.mall.entity.TbUserComment;
import com.group7.mall.resp.UserGoodsPageResponse;
import com.group7.mall.req.UserAddCommentRequest;
import com.group7.mall.req.UserCommentPageRequest;
import com.group7.mall.service.CommentService;
import com.group7.mall.util.Base64Utils;
import com.group7.mall.util.UploadFileToRemoteUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("user-comments")
@Slf4j
public class CustomerCommentController {

    @Resource
    private CommentService commentService;

    @PostMapping("add-comment")
    public Result<Integer> addComment(@RequestBody UserAddCommentRequest userAddCommentRequest) throws IOException {
        return Result.ok(commentService.insert(userAddCommentRequest));
    }

    @PostMapping("show-comment-discussion")
    public Result<CommentDiscussionDto> showCommentDiscussion(@RequestBody TbUserComment tbUserComment) {
        return Result.ok(commentService.showCommentDiscussion(tbUserComment));
    }

    @PostMapping("show-goods-comments")
    public Result<UserGoodsPageResponse<UserCommentDto>> showGoodsComments(@RequestBody UserCommentPageRequest userCommentPageRequest) {
        userCommentPageRequest.setCommentType(0);
        userCommentPageRequest.setHavingAppendedComment(null);
        userCommentPageRequest.setHavingImage(null);

        switch (userCommentPageRequest.getCommentSort()) { // 假设`someValue`是您要检查的值
            // 带图评价
            case 4:
                userCommentPageRequest.setHavingImage(1);
                userCommentPageRequest.setCommentSort(null);
                break;
                // 全部
            case 5:
                userCommentPageRequest.setHavingAppendedComment(null);
                userCommentPageRequest.setCommentSort(null);
                userCommentPageRequest.setHavingImage(null);
                break;
        }
        return Result.ok(this.commentService.showGoodsComments(userCommentPageRequest));
    }

    @PostMapping("add-comment-discussion")
    public Result<TbCommentDiscussion> addCommentDiscussion(@RequestBody TbCommentDiscussion tbCommentDiscussion) {
        return Result.ok(commentService.addCommentDiscussion(tbCommentDiscussion));
    }

    @PostMapping("upload-image-message")
    public Result<String> uploadImageMessage(@RequestBody MessageDto messageDto) throws IOException {
        MultipartFile multipartFile = Base64Utils.convertBase64ToMultipart(messageDto.getImageBase64Code(), messageDto.getImageName());
        JsonResult jsonResult = UploadFileToRemoteUtils.uploadFileToRemoteServer(multipartFile, "/opt/docker/apache/www/images/test");
        System.out.println("ok");
        return Result.ok("http://117.72.40.8/images/test/" + messageDto.getImageName());
    }
}
